package com.goods.controller.business;

import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.Product;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@Api(tags = "业务管理-出库记录相关接口")
@RestController
@RequestMapping("/business/consumer")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    //物资去处列表显示
/*    @GetMapping("/findConsumerList")
    public ResponseBean<PageVO<ConsumerVO>> findConsumerList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize,
            ConsumerVO consumerVO){

        PageVO<ConsumerVO>  consumerVOPageVO= consumerService.findConsumerList(pageNum,pageSize,consumerVO);
        return ResponseBean.success(consumerVOPageVO);
    }*/

    //物资去处列表显示
    @GetMapping("/findConsumerList")
    public ResponseBean<PageVO<ConsumerVO>> findConsumerList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "pageSize", defaultValue = "6") Integer pageSize,
                                                       ConsumerVO consumerVO){
        List<ConsumerVO> products = consumerService.findConsumerList(consumerVO.getAddress(),
                consumerVO.getContact(),consumerVO.getName());
        List<ConsumerVO> page = ListPageUtils.page(products, pageSize, pageNum);
        PageVO pageVO = null;
        if(page != null){
            pageVO = new PageVO(page.size(), page);
            return ResponseBean.success(pageVO);
        }else {
            List<Product> list = new ArrayList<>();
            pageVO = new PageVO(0,list);
            return ResponseBean.success(pageVO);
        }
    }

    //物资去处回显
    @GetMapping("/edit/{id}")
    public ResponseBean<Consumer> edit(@PathVariable Long id){
        Consumer consumer = consumerService.edit(id);
        return ResponseBean.success(consumer);
    }

    //物资去处修改
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id , @RequestBody @Validated Consumer consumer ){
        consumerService.update(id,consumer);
        return ResponseBean.success();
    }

    //物资去处删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        consumerService.delete(id);
        return ResponseBean.success();
    }

}
